package 链表;

/* https://leetcode-cn.com/problems/middle-of-the-linked-list/ */
public class _876_链表的中间节点 {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    // 解题核心：快慢指针法，让fast走两步，slow走一步，那么当fast.next = null那么slow一定在中间
    public  static ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        if (head == null){return null;}
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
